package com.num2.service.lkw;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import com.num2.dao.lkw.IDepositDao;
import com.num2.form.lkw.Deposit_Money;

@Service
public class DepositService {
	@Resource
	private SqlSessionFactory sqlSessionFactory;
	
	public boolean insertMoney(Deposit_Money dm,HttpSession session) {
		//打开session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过 注解 获取 接口实现类的对象
       IDepositDao depositdao = sqlSession.getMapper(IDepositDao.class);
       
       //通过session得到的username获取user_id
       int userid = depositdao.selectName((String) session.getAttribute("user_name"));
       dm.setUser_id(userid);
       
       //通过user_id获取余额balance
       int balan= depositdao.select(dm.getUser_id());
       //将获取的存款金额转变为字符串数组,判断是否每个字符都为数字
       char num[] = dm.getIncome_expend().substring(1).toCharArray();
       for(int i=0;i<num.length;i++){
			if(!Character.isDigit(num[i])){
			    return false;
			}
       }
			dm.setBalance(balan);
//		    System.out.println(balan);
			//增加一条用户存款的记录
			depositdao.add(dm);
		    //提交session
		    sqlSession.commit();
//		    sqlSession.close();
			return true;
	}
	
	

	public SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}

	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}
	

}
